package com.alarm.app.receiver;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.alarm.app.AlarmApplication;
import com.alarm.app.Tools.Constants;
import com.alarm.app.Tools.Ctool;


/**
 * Desc:
 * 轮询通知的广播接受器,主要用于处理三种广播
 * <p>
 *     </>
 * Created by Hym on 2016/1/30 15:04
 * Modified by
 * Modification：
 */
public class AlarmReceiver extends BroadcastReceiver {

    
    public static final String LOOP_TAG = "loop";
    
    /**
     * 通知轮询requestCode
     */
    public static final int NOTIFICATION_REQUEST_CODE  = 20;
    
	@Override
	public void onReceive(Context context, Intent intent) {
//        final String action = intent.getAction();
//
//        // 监听网络状态
//        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
//            //①获取到网络状态改变的广播
//            final ConnectivityManager connectivityManager = (ConnectivityManager) context
//                    .getSystemService(Context.CONNECTIVITY_SERVICE);
//            final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
//
//            if (info != null && info.isAvailable()) {
//                String name = info.getTypeName();
//                startNotificationAlarm();
//            } else {
//                return;
//            }
//        }
//
//        if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
//            //②接受到开机广播
//            startNotificationAlarm();
//
//            //启动fork进程
//            final Intent forkIntent = new Intent(DfReceiver.ACTION_RESTART_SERVICE_START_FORK);
//            context.sendBroadcast(forkIntent);
//       } else if (Constants.RECEIVER_LOOP_NOTIFICATION.equals(action)) {
//            //③获取到轮询通知的广播
//           startNotificationAlarm();
//           // 获取通知
//           getNotification();
//
//       }
//
	}
	
	/**
     * 开启发送广播，执行轮询闹钟
     */
	public static void startNotificationAlarm() {
	    final long interval = Ctool.isRunningForeground() ? 1000 : 3000;
	    startNotificationAlarm(interval);
	}

    /**
     * <p>
     * <li> 开启闹钟，间隔interval的时间，执行pendingIntent，在此是发送一个广播，并且有当前的类
     * （广播接收器的类）接受，并回调onReceive方法，执行相应的操作。
     * <li>onReceive方法中接受广播，并重启闹钟，同事拉取服务器上的消息，以实现消息的轮询
     * <p/>
     * 重启闹钟：如果有相同的peddingIntent的闹钟被启动过，那么重启后，前一个闹钟会被取消
     * @param interval 开启闹钟后，间隔多长时间后，执行的pendingIntent
     */
	public static void startNotificationAlarm(final long interval) {

        final Context mContext = AlarmApplication.getInstance();
        final int requestCode = NOTIFICATION_REQUEST_CODE;
        
        final Intent requestIntent = new Intent();
        requestIntent.setClass(mContext, AlarmReceiver.class);
        requestIntent.setAction(Constants.RECEIVER_LOOP_NOTIFICATION);

        final PendingIntent pendingIntent = PendingIntent.getBroadcast(
                mContext, requestCode, requestIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        final AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + (interval * 1000) , pendingIntent);
    }
	
	/**
     * 关闭通知轮询
     */
    public static void stopNotificationAlarm() {
        final Context mContext = AlarmApplication.getInstance();
        final int requestCode = NOTIFICATION_REQUEST_CODE;
        
        final Intent intent = new Intent();
        intent.setClass(mContext, AlarmReceiver.class);
        intent.setAction(Constants.RECEIVER_LOOP_NOTIFICATION);
        
        final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
                requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        final AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);
    }
    
    private static final long lastIntervalTime = 2 * 1000;
    
    /**
     * 立即刷新轮询消息
     */
    public static void refreshLoopMsg() {
//        final YYPreferences pref = YYPreferences.getInstance();
//
//        final AlarmApplication application = AlarmApplication.getInstance();
//        final long lastRefreshMsgTime = application.getLastRefreshMsgTime();
//        if (System.currentTimeMillis() - application.getLastRefreshMsgTime() < lastIntervalTime) {
//        } else {
//            stopNotificationAlarm();
//
//            //TODO:
//
//            startNotificationAlarm();
//        }
    }
    
    /**
     * 获取轮询消息，此处模拟假消息
     */
    private void getNotification() {

    }

}
